package com.nulldev.util.threading.v3;

import com.nulldev.util.math.MathUtil;
import com.nulldev.util.threading.v2.ThreadPriority;
import com.nulldev.util.threading.v2.nThread;

public abstract class Worker implements IWorker {

	private final int id = MathUtil.randInt(0, 100000000);
	private boolean alive;
	public Worker() {
	}
	
	public abstract void execute();
	
	@Override
	public void run() {
		this.alive = true;
		this.execute();
		this.alive = false;
	}
	
	@Override
	public int getID() {
		return this.id;
	}

	@Override
	public boolean isAlive() {
		return this.alive;
	}

	@Override
	@Deprecated
	public void shutdown() {
	}
	
	/**
	 * Converts Worker back to threadIt! API v2.
	 * @returns nThread with NORMAL priority
	 */
	public nThread toLegacy() {
		return new nThread(this, "$worker.ported(" + this.getID() + ")", ThreadPriority.NORMAL);
	}

}
